package ch.mustaffah.aoide.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class AoideKeyBordPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JCheckBox[][] checkBoxes;

	public AoideKeyBordPanel() {
		initializeView();
	}

	private void initializeView() {
		checkBoxes = new JCheckBox[12][8];
		setLayout(new GridLayout(9, 13));
		for (int i = 0; i < 12; i++) {
			add(getClearColumnButton(i));
		}
		add(getSelectAllButton());
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 12; j++) {
				JCheckBox box = new JCheckBox();
				checkBoxes[j][i] = box;
				add(box);
			}
			add(getClearOvtaveButton(i));
		}
		setBackground(Color.white);
		setVisible(true);
	}

	private JToggleButton getSelectAllButton() {
		final JToggleButton button = new JToggleButton("O");
		button.setPreferredSize(new Dimension(15, 15));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (button.isSelected()) {
					selectAll(true);
				} else {
					selectAll(false);
				}
			}
		});
		return button;
	}

	private JToggleButton getClearOvtaveButton(final int line) {
		final JToggleButton button = new JToggleButton("x");
		button.setPreferredSize(new Dimension(15, 15));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (button.isSelected()) {
					setOctaveSelection(line, true);
				} else {
					setOctaveSelection(line, false);
				}
			}
		});
		return button;
	}

	private JToggleButton getClearColumnButton(final int line) {
		final JToggleButton button = new JToggleButton("x");
		button.setPreferredSize(new Dimension(15, 15));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (button.isSelected()) {
					setColumnSelection(line, true);
				} else {
					setColumnSelection(line, false);
				}

			}
		});
		return button;
	}

	public boolean[] getSelectionPattern(int octave) {
		octave = 8 - octave;
		boolean[] selectionArr = new boolean[12];
		for (int i = 0; i < checkBoxes.length; i++) {
			selectionArr[i] = checkBoxes[i][octave].isSelected();
		}
		return selectionArr;
	}

	public void selectAll(boolean state) {
		for (int i = 0; i < checkBoxes.length; i++) {
			for (int j = 0; j < checkBoxes[0].length; j++) {
				checkBoxes[i][j].setSelected(state);
			}
		}
	}

	public void setOctaveSelection(int octave, boolean state) {
		for (int i = 0; i < checkBoxes.length; i++) {
			checkBoxes[i][octave].setSelected(state);
		}
	}

	public void setColumnSelection(int column, boolean state) {
		for (int i = 0; i < checkBoxes[0].length; i++) {
			checkBoxes[column][i].setSelected(state);
		}
	}

	public boolean[][] getSelectionMatrix() {
		boolean[][] result = new boolean[checkBoxes.length][checkBoxes[0].length];
		for (int i = 0; i < checkBoxes.length; i++) {
			for (int j = 0; j < checkBoxes[0].length; j++) {
				result[i][j] = checkBoxes[i][j].isSelected();
			}
		}
		return result;
	}

	public void setSelectionPattern(boolean[][] selectionMatrix) {
		if (selectionMatrix.length == checkBoxes.length
				&& selectionMatrix[0].length == checkBoxes[0].length) {
			for (int i = 0; i < checkBoxes.length; i++) {
				for (int j = 0; j < checkBoxes[0].length; j++) {
					checkBoxes[i][j].setSelected(selectionMatrix[i][j]);
				}
			}
		}
	}
}
